package com.bocai.kelala.ui.travel.stroke.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.bigkoo.pickerview.listener.OnDismissListener;
import com.bocai.kelala.App;
import com.bocai.kelala.R;
import com.bocai.kelala.base.BaseActivity;
import com.bocai.kelala.bean.CheckLineBean;
import com.bocai.kelala.bean.MyAddressBean;
import com.bocai.kelala.ui.travel.main.act.MainActivity;
import com.bocai.kelala.ui.travel.set.act.CarTeamsActivity;
import com.bocai.kelala.ui.travel.stroke.contract.PublishStrokeContract;
import com.bocai.kelala.ui.travel.stroke.module.PublishStrokeModule;
import com.bocai.kelala.ui.travel.stroke.presenter.PublishStrokePresenter;
import com.bocai.kelala.util.DateUtil;
import com.bocai.kelala.widget.CarNumDialog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/13.
 * 发布行程
 */

public class PublishStrokeActivity extends BaseActivity implements PublishStrokeContract.view{
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.ll_user_car_time)
    LinearLayout llUserCarTime;
    @BindView(R.id.tv_car_num)
    TextView tvCarNum;
    @BindView(R.id.ll_car_num)
    LinearLayout llCarNum;
    @BindView(R.id.check_agree)
    CheckBox checkAgree;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.btn_publish)
    AppCompatButton btnPublish;
    CheckLineBean bean;
    @Inject
    PublishStrokePresenter presenter;
    CarNumDialog carNumDialog;
    TimePickerView pickerView;
    String priceId;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_cat_teams)
    TextView tvCatTeams;

    @Override
    public int getLayoutId() {
        return R.layout.act_publish_stroke;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new PublishStrokeModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("发布行程");
        initBack(imgBack,llBack);
        bean = (CheckLineBean) getIntent().getSerializableExtra("data");
        setDataView();
        presenter.optionalSeats();
    }

    @OnClick({R.id.ll_car_num,R.id.ll_user_car_time,R.id.btn_publish,R.id.tv_cat_teams})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.ll_car_num:
                carNumDialog.show();
                break;
            case R.id.ll_user_car_time:
                pickerView.show();
                break;
            case R.id.btn_publish:
                if (TextUtils.isEmpty(tvStartTime.getText().toString())){
                    showErrorTip("请选择用车开始时间");
                }else if (TextUtils.isEmpty(tvCarNum.getText().toString())){
                    showErrorTip("请选择用车座位数");
                }else if (!checkAgree.isChecked()){
                    showErrorTip("请阅读并同意服务协议");
                }else if (DateUtil.setStringToData(tvStartTime.getText().toString()).getTime() - new Date().getTime() <= 6*60*60*1000){
                    showErrorTip("出行时间小于6小时");
                }else {
                    presenter.publishStroke(priceId,tvStartTime.getText().toString());
                }
                break;
            case R.id.tv_cat_teams:
                startActivity(CarTeamsActivity.class);
                break;
        }
    }
    public void setDataView() {
        pickerView = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                if (date.getTime() - new Date().getTime() <= 6*60*60*1000){
                    showErrorTip("出行时间小于6小时");
                }else {
                    tvStartTime.setText(DateUtil.dateToDateTimeString(date));
                }
                if (!TextUtils.isEmpty(tvStartTime.getText().toString())
                        &&!TextUtils.isEmpty(tvCarNum.getText().toString())){
                    btnPublish.setEnabled(true);
                    btnPublish.setTextColor(getResources().getColor(R.color.color_white));
                }else {
                    btnPublish.setEnabled(false);
                    btnPublish.setTextColor(getResources().getColor(R.color.text_color));
                }

            }
        })
                .setType(TimePickerView.Type.ALL)
                .build();
    }

    @Override
    public void checkLineSuccess(CheckLineBean bean) {

    }

    @Override
    public void publishStrokeSuccess() {
        showErrorTip("发布成功");
        setResult(15);
        startActivity(new Intent(this,MainActivity.class));
        finish();
    }

    @Override
    public void optionalSeatsList(final List<Integer> list) {
        carNumDialog = new CarNumDialog(this).builder().setList(list, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                boolean isValue = false;

                for (int i = 0;i<bean.getPriceList().size();i++){
                    if (bean.getPriceList().get(i).getSeat() == list.get(position)){
                        isValue = true;
                        priceId = bean.getPriceList().get(i).getPriceId();
                         tvMoney.setText("价格为"+bean.getPriceList().get(i).getPrice()+"元");
                    }
                }
                if (isValue){
                    tvCarNum.setText(list.get(position)+"");
                }else {
                    showErrorTip("请联系后台添加相对于的车位数");
                }
                if (!TextUtils.isEmpty(tvStartTime.getText().toString())
                        &&!TextUtils.isEmpty(tvCarNum.getText().toString())){
                    btnPublish.setEnabled(true);
                    btnPublish.setTextColor(getResources().getColor(R.color.color_white));
                }else {
                    btnPublish.setEnabled(false);
                    btnPublish.setTextColor(getResources().getColor(R.color.text_color));
                }
                carNumDialog.dismiss();
            }
        });
    }

    @Override
    public void originAreasList(List<MyAddressBean> list) {

    }

    @Override
    public void arriveAreasList(List<MyAddressBean> list) {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {

        if (msg.equals("用户授权失败")){
            msg = "您的登录已失效，请重新登录";
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
        showShortToast(msg);
    }
}
